
<template>
  <div>
    <!-- 查询参数及按钮部分 -->
    <div class="filter-container">
      <el-dialog  v-dialogDrag  
        :close-on-click-modal="false"
        :title="dialogStatus"
        :visible.sync="dialogEditFormVisible"
        style="padding:0px;"
        width="50%"
      >
        <el-form
          ref="dataForm"
          class="dialog-Backgroundcolor"
          :model="listQuery"
          label-position="right"
          label-width="120px"
        >
          <el-row>
            <el-row style="margin-left:-30px">
              <el-col :span="12">
                <el-form-item label="工资类别：">
                  <el-select
                    v-model="listQuery.gzlb"
                    style="margin-left:-12px"
                    class="filter-item inputWidth"
                    clearable
                    placeholder="--请选择--"
                  >
                    <el-option
                      v-for="item in gzlbs"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号码位数:">
                  <el-select
                    v-model="listQuery.idLength"
                    class="filter-item inputWidth"
                    clearable
                    placeholder="--请选择--"
                  >
                    <el-option
                      v-for="item in idLengths"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-button
              type="primary"
              style="float:right;"
              @click="handleFind"
            >
              查询
            </el-button>

          </el-row>
        </el-form>
      </el-dialog>
    </div>

    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      fit
      style="margin-top: 10px;"
      :height="tableHeight"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80"
      />
      <el-table-column
        v-if="tabPane !=='all'"
        label="核对状态"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.idenIsVerify | idenIsVerifyFilter() }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column v-if="tabPane !=='all'" label="id" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id}}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="姓名"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.personName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="身份证号码"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.idenNo  }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="性别"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.sexCode | sexFilter(that) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="备注"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.remarks }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 编辑页面部分 -->
    <el-dialog  v-dialogDrag  
      :close-on-click-modal="false"
      :title="dialogStatus"
      :visible.sync="dialogFormVisible"
      width="40%"
    >
      <el-form
        ref="dataForm"
        class="dialog-Backgroundcolor"
        :rules="dataFormRules"
        :model="formData"
        label-position="top"
        label-width="130px"
        :disabled="formDsiabled"
        size="small"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="身份证号码："
              prop="idenNo"
            >
              <el-input
                v-model="formData.idenNo"
                maxlength="18"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="备  注："
              prop="remarks"
            >
              <el-input
                v-model="formData.remarks"
                maxlength="100"
              />
              <el-row>
                <el-col
                  v-if="operate === 'edit'"
                  :span="24"
                  style="text-align: cenetr; margin-left:-80px;margin-top:10px"
                >
                  <el-button
                    type="primary"
                    :loading="updateLoading"
                    @click="updateData()"
                  >
                    保存
                  </el-button>
                  <el-button @click="dialogFormVisible = false">
                    取消
                  </el-button>
                </el-col>

              </el-row>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPersonOperateList,
  checkPassPersonOperateList,
  undoCheckPersonOperateList,
  updatePersonOperateList,
  undoUpdatePersonOperateListID,
  forReviewPersonOperateList,
  undoForReviewPersonOperateList
} from '@/api/personInfoMaintenance/personIDCheck'
import { getSex } from '@/api/baseInfo'
// import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  name: 'Unit',
  components: { Pagination, tableEmpty },
  filters: {
    // 性别过滤器
    sexFilter(type, that) {
      const sexLists = that.sexLists
      for (const item in sexLists) {
        if (sexLists[item].code === type) {
          return sexLists[item].name
        }
      }
    },
    // 待核对状态过滤器
    idenIsVerifyFilter(type) {
      if (type === '0') {
        return '未核对'
      } else if (type === '1') {
        return '已核对'
      }
    },
    //号码脱敏显示
    hideMiddle(val) {
      if(val!== undefined){
        return `${val.substring(0,3)}****${val.substring(val.length-3)}`
      }else{
        return val
      }
    }
  },
  props: ['tabPane'],
  data() {
    return {
      tableHeight: null,
      dialogStatus: '',
      dialogEditFormVisible: false,
      that: this,
      list: null,
      total: 0,
      listLoading: true,
      checkPassLoading: false,
      undoCheckLoading: false,
      forReviewLoading: false,
      updateLoading: false,
      undoUpdateLoading: false,
      undoForReviewLoading: false,
      listQuery: {
        page: 1,
        limit: 200,
        title: undefined,
        type: 'idCheck',
        flag: 'all',
        idLength: '',
        gzlb: '',
        agencyId: this.$store.getters.userInfo.agencyId
      },
      formDsiabled: false,
      formData: {
        id: '',
        personName: '',
        idenNo: '',
        sexCode: '',
        remarks: '',
        meritPayCode: '',
        idenIsVerify: ''
      },
      sexLists: [], // 性别
      idenIsVerifyList: [], // 核对
      idLengths: [
        { code: '15', name: '十五位' },
        { code: '18', name: '十八位' }
      ],
      gzlbs: [
        { code: '0', name: '行政人员' },
        { code: '1', name: '事业人员' },
        { code: '11', name: '离退休人员' }
      ],
      personOperate: [],

      operate: 'edit',
      dialogFormVisible: false,
      dataFormRules: {
        idenNo: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' },
          {
            pattern: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)/,
            message: '证件号码格式有误！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  watch: {
    // 表格的数据数组
    list(val) {
      this.doLayout()
    }
  },
  // 加载后运行
  created() {
    // 加载初始数据
    this.initBaseInfo()
    // 获取参数（全部、未送审、已送审、被退回）
    this.listQuery.flag = this.tabPane
    // 加载获取全部数据
    this.getList()
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  methods: {
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 210
    },
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 查询
    query() {
      console.log(111)
      this.dialogStatus = '查询'
      this.dialogEditFormVisible = true
    },
    // 获取全部数据
    getList() {
      this.listLoading = true
      getPersonOperateList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 更新
    handleFilter() {
      this.getList()
    },
    handleFind() {
      this.listQuery.page = 1
      this.getList()
      this.dialogEditFormVisible = false
    },

    // 获取基础数据
    initBaseInfo() {
      getSex().then(response => {
        this.sexLists = Object.assign({}, response.data)
      })
    },
    // 核对通过
    checkPass() {
      // 判断是否选择数据
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择数据',
          type: 'warning',
          duration: 4000
        })
        this.$emit('checkPassCallBack')
        return
      }
      this.personOperate.splice(0, this.personOperate.length)
      // 提示框确认
      // this.$confirm('确定核对通过吗？', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      selection.forEach(item => {
        this.personOperate.push(item)
      })
      checkPassPersonOperateList(this.personOperate).then(response => {
        this.dialogFormVisible = false
        this.$message({
          showClose: true,
          message: '核对成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('checkPassCallBack')
        // 更新
        this.handleFilter()
      })
      // })
      // .catch(() => {
      //   this.$emit('checkPassCallBack')
      // })
    },
    // 撤销核对
    undoCheck() {
      this.personOperate.splice(0, this.personOperate.length)
      // 判断是否选择数据
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择数据',
          type: 'warning',
          duration: 4000
        })
        this.$emit('undoCheckCallBack')
        return
      }
      // 循环检查数据
      let state = true
      for (let index = 0; index < selection.length; index++) {
        const element = selection[index]
        this.personOperate.push(element)
        if (element.idenIsVerify === '0') {
          state = false
          this.$message({
            showClose: true,
            message: '未核对的数据不能撤销不能撤销核对',
            type: 'warning',
            duration: 4000
          })
          this.$emit('undoCheckCallBack')
          break
        }
      }

      if (state) {
        this.undoCheckLoading = true
        // 提示框确认
        // this.$confirm('确定撤销吗？', {
        //   closeOnClickModal: false,
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // })
        //   .then(() => {
        undoCheckPersonOperateList(this.personOperate).then(response => {
          this.dialogFormVisible = false
          this.$message({
            showClose: true,
            message: '撤销成功',
            type: 'success',
            duration: 4000
          })
          this.$emit('undoCheckCallBack')
          // 更新
          this.handleFilter()
        })
        // })
        // .catch(() => {
        //   this.$emit('undoCheckCallBack')
        // })
      }
    },
    // 修改
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.formData)
          this.updateLoading = true
          updatePersonOperateList(tempData).then(response => {
            this.dialogFormVisible = false
            this.$message({
              showClose: true,
              message: '修改成功',
              type: 'success',
              duration: 4000
            })
            this.updateLoading = false
            // 更新
            this.handleFilter()
          })
        }
      })
    },
    // 修改弹框
    toUpdate() {
      // 判断是否选择数据
      const selection = this.$refs.singalTable.selection
      if (selection.length !== 1) {
        this.$message({
          showClose: true,
          message: '请选一条择数据',
          type: 'warning',
          duration: 4000
        })
        this.$emit('toUpdateCallBack')
        return
      }

      this.formDsiabled = false
      this.operate = 'edit'
      this.dialogStatus = '身份证信息'
      this.dialogFormVisible = true
      this.formData = Object.assign({}, selection[0])
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 撤销修改
    undoUpdate() {
      this.personOperate.splice(0, this.personOperate.length)
      // 判断是否选择数据
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择数据',
          type: 'warning',
          duration: 4000
        })
        this.$emit('undoUpdateCallBack')
        return
      }
      let state = true
      for (let index = 0; index < selection.length; index++) {
        const element = selection[index]
        this.personOperate.push(element)
        if (element.operateCode !== 'checkIdNo') {
          state = false
          this.$message({
            showClose: true,
            message: '没有修改的信息不能撤销修改',
            type: 'warning',
            duration: 4000
          })
          this.$emit('undoUpdateCallBack')
          break
        }
      }

      if (state) {
        this.undoUpdateLoading = true
        // 提示框确认
        // this.$confirm('确定撤销吗？', {
        //   closeOnClickModal: false,
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // })
        //   .then(() => {
        const tempData = Object.assign({}, selection[0])
        undoUpdatePersonOperateListID(tempData).then(response => {
          this.dialogFormVisible = false
          this.$message({
            showClose: true,
            message: '撤销成功',
            type: 'success',
            duration: 4000
          })
          this.$emit('undoUpdateCallBack')
          // 更新
          this.handleFilter()
        })
        // })
        // .catch(() => {
        //   this.$emit('undoUpdateCallBack')
        // })
      }
    },
    // 送审
    forReview() {
      this.personOperate.splice(0, this.personOperate.length)
      // 判断是否选择数据
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择数据',
          type: 'warning',
          duration: 4000
        })
        this.$emit('forReviewCallBack')
        return
      }
      // 循环检查数据
      let state = true
      for (let index = 0; index < selection.length; index++) {
        const element = selection[index]
        this.personOperate.push(element)
        if (element.idenIsVerify === '0') {
          state = false
          this.$message({
            showClose: true,
            message: '核对通过才能送审',
            type: 'warning',
            duration: 4000
          })
          this.$emit('forReviewCallBack')
          break
        }
      }

      if (state) {
        // 提示框确认
        // this.$confirm('确定送审吗？', {
        //   closeOnClickModal: false,
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // })
        //   .then(() => {
        forReviewPersonOperateList(this.personOperate).then(response => {
          this.dialogFormVisible = false
          this.$message({
            showClose: true,
            message: '送审成功',
            type: 'success',
            duration: 4000
          })
          this.$emit('forReviewCallBack')
          // 更新
          this.handleFilter()
        })
        // })
        // .catch(() => {
        //   this.$emit('forReviewCallBack')
        // })
      }
    },
    // 撤销送审
    undoForReview() {
      // 判断是否选择数据
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择数据',
          type: 'warning',
          duration: 4000
        })
        this.$emit('undoForReviewCallBack')
        return
      }
      this.personOperate.splice(0, this.personOperate.length)
      // 提示框确认
      // this.$confirm('确定撤销吗？', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      selection.forEach(item => {
        this.personOperate.push(item)
      })

      undoForReviewPersonOperateList(this.personOperate).then(response => {
        this.$message({
          showClose: true,
          message: '撤销成功',
          type: 'success',
          duration: 4000
        })
        this.$emit('undoForReviewCallBack')
        // 更新
        this.handleFilter()
      })
      // })
      // .catch(() => {
      //   this.$emit('undoForReviewCallBack')
      // })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/border.scss"; /*引入公共样式*/
.addForm {
  text-align: center;
  .selectSpan {
    margin-right: 40%;
    font-size: 18px;
    font-weight: 500;
  }
  .addSelectRow {
    margin-top: 3%;
  }
  .addButRow {
    margin-top: 5%;
  }
  .addTitpsRow {
    margin-top: 8%;
    margin-bottom: 8%;
    .addTitps {
      font-size: 18px;
      color: #ff0000;
      font-weight: 600;
    }
  }
}
</style>
